package com.sfeir.poc.portal.gui.client.services;

import java.util.Iterator;
import java.util.List;

import com.google.gwt.user.client.Window;
import com.sfeir.poc.portal.gui.client.EventAwareClass;
import com.sfeir.poc.portal.gui.client.events.ApplicationEvent;
import com.sfeir.poc.portal.gui.client.events.ApplicationEventType;
import com.sfeir.poc.portal.gui.client.objects.News;
import com.sfeir.poc.portal.gui.client.objects.WidgetDescription;
import com.sfeir.poc.portal.gui.client.services.feed.Entrie;
import com.sfeir.poc.portal.gui.client.services.feed.Feed;
import com.sfeir.poc.portal.gui.client.services.feed.FeedListener;
import com.sfeir.poc.portal.gui.client.util.NewsWidgetBuilder;
import com.sfeir.poc.portal.gui.client.util.RssContentParser;

public class NewsService extends EventAwareClass implements FeedListener{

	private List<String[]> newsList = null;
	private RssContentParser rssContentParser = new RssContentParser();
	private NewsWidgetBuilder newsWidgetBuilder = new NewsWidgetBuilder();

	/**
	 * This function is used to get content from a remote host using the Google Feed API
	 * @param widgetDescription
	 */
	public void getNews(final WidgetDescription widgetDescription) {
		final Feed feed = new Feed();
		String url = "";
		/*Google Feed API is using a cache, in order to bypass this one, we
		  use a hack by passing some data after the url we need
		  Some host like feedburner does not support this hack so we have
		  to deal with that*/
		if (widgetDescription.getType().compareTo("rss") == 0) {
			url = widgetDescription.getUrl();
		} else {
			url = widgetDescription.getUrl() + "#" + System.currentTimeMillis();
		}
		feed.getFeed(url, this, widgetDescription);
	}

	/**
	 * This function is called on the feed.getFeed callback in case of failure
	 */
	public void onFailure() {
		Window.alert("echec");	
	}

	/**
	 * This function is called on the feed.getFeed callback in case of success
	 */
	public void onSuccess(Feed feed) {
		String result = "";

		Entrie entrie = null;
		for (Iterator<Entrie> iterator = feed.getEntries().iterator(); iterator.hasNext();) {
			entrie = (Entrie) iterator.next();
			result += entrie.getTitle() + ", ";
			result += entrie.getContent();
			result += "\n";
		}

		final WidgetDescription widgetDescription = feed.getWidgetDescription();
		widgetDescription.setContent(result);

		widgetDescription.setFeed(feed);
		if (widgetDescription.getType().compareTo("spreadsheet") == 0) {
			newsList = rssContentParser.parse(result);
			List<News> news = newsWidgetBuilder.getNewsList(newsList);
			widgetDescription.setNews(news);
		}

		if (widgetDescription.getType().compareTo("slideshow") == 0) {
			newsList = rssContentParser.parse(result);
			List<News> news = newsWidgetBuilder.getNewsList(newsList);
			widgetDescription.setNews(news);
		}

		fireActionEvent(ApplicationEventType.WIDGET_UPDATED, widgetDescription);
	}

	/**
	 * Implementation of the onActionFired method from FeedListener
	 * Is used to catch events and launch task when 
	 */
	public void onActionFired(ApplicationEvent event) {
		super.onActionFired(event);
		if (event.getType().equals(ApplicationEventType.GET_FEED)) {
			getNews((WidgetDescription)event.getContext());
		}
	}
}
